var saveFlag = '';
var endQes = '';

function getQuestion(qesCode, subQes)
{
	subQes = subQes == undefined ? '' : subQes;
	var qes = new Object();
	var txtList = $('#td_' + qesCode + ' input:text[fitType="normal"]');
	if(isNotNull(subQes))
	{
		txtList = $('#td_' + qesCode + ' input:text[fitType="normal"][subQes="' + subQes + '"]');
	}
	if(txtList.length == 0)
	{
		alert('脚本有误，' + qesCode + '不存在该题或子题。');
	}
	qes.txtList = txtList;
	qes.qesCode = qesCode;
	qes.subQes = subQes;
	
	qes.val = function(){
		return $(txtList.get(0)).val();
	};
	
	qes.values = function(){
		var valArray = new Array();
		var len = txtList.length;
		for(var i = 0; i < len; i++)
		{
			var txtx = $(txtList.get(i));
			valArray.push(txtx.val());
		}
		return valArray;
	};
	
	qes.notNullValues = function(){
		var valArray = new Array();
		var len = txtList.length;
		for(var i = 0; i < len; i++)
		{
			var txtx = $(txtList.get(i));
			if(isNotNull(txtx.val()))
			{
				valArray.push(txtx.val());
			}
		}
		return valArray;
	};
	
	qes.compareWith = function(compute, x){
		if(';>;>=;<;<=;==;!=;'.indexOf(';'+compute+';') == -1)
		{
			showMsg('定义错误，运算符有误。');
			return false;
		}
		var flag = false;
		if(!isNotNull(this.notNullValues()[0]))
		{
			flag = false;
		}
		else
		{
			flag = eval(this.notNullValues()[0] + compute + x);
		}
		return flag;
	};
	
	qes.include = function(arr){
		var arrLen = arr.length;
		var vals = this.values();
		var valLen = vals.length;
		var inc = 0;
		var blankLen = 0;
		for(var i = 0; i < valLen; i++)
		{
			if(!isNotNull(vals[i]))
			{
				blankLen++;
			}
		}
		for(var i = 0; i < valLen; i++)
		{
			for(var j = 0; j < arrLen; j++)
			{
				if(isNotNull(vals[i]) && vals[i] == arr[j])
				{
					inc++;
					break;
				}
			}
		}
		var event = window.event;
		var keyFlag = event == null ? false : ('13' == event.keyCode);
		if(inc != arrLen && ('save' == saveFlag || 'error' == saveFlag || keyFlag))
		{
			return false;
		}
		if(blankLen < (arrLen - inc))
		{
			return false;
		}
		return true;
	};
	
	qes.onlyInclude = function(arr){
		var arrLen = arr.length;
		var vals = this.values();
		var valLen = vals.length;
		var inc = 0;
		for(var i = 0; i < valLen; i++)
		{
			for(var j = 0; j < arrLen; j++)
			{
				if(isNotNull(vals[i]) && !arrayInclude(arr, vals[i]))
				{
					return false;
				}
				else if(vals[i] == arr[j])
				{
					inc++;
					break;
				}
			}
		}
		var event = window.event;
		var keyFlag = event == null ? false : ('13' == event.keyCode);
		if(inc != arrLen && ('save' == saveFlag || 'error' == saveFlag || keyFlag))
		{
			return false;
		}
		return true;
	};
	
	qes.exclude = function(arr){
		var vals = this.notNullValues();
		var valLen = vals.length;
		for(var i = 0; i < valLen; i++)
		{
			if(arrayInclude(arr, vals[i]))
			{
				return false;
			}
		}
		return true;
	};
	
	qes.excludeFromQes = function(qesCode, optionIndex){
		optionIndex = optionIndex == undefined ? '' : optionIndex;
		var fromQes = getQuestion(qesCode, optionIndex);
		var arr = fromQes.notNullValues();
		var vals = this.notNullValues();
		var valLen = vals.length;
		for(var i = 0; i < valLen; i++)
		{
			if(arrayInclude(arr, vals[i]))
			{
				return false;
			}
		}
		return true;
	};
	
	qes.sameAs = function(fromQesCode, subQes){
		subQes = subQes == undefined ? '' : subQes;
		var arr = getQuestion(fromQesCode, subQes).notNullValues();
		var arrLen = arr.length;
		var vals = this.values();
		var valLen = vals.length;
		var inc = 0;
		var blankLen = 0;
		for(var i = 0; i < valLen; i++)
		{
			if(!isNotNull(vals[i]))
			{
				blankLen++;
			}
		}
		for(var i = 0; i < valLen; i++)
		{
			for(var j = 0; j < arrLen; j++)
			{
				if(isNotNull(vals[i]) && vals[i] == arr[j])
				{
					inc++;
					break;
				}
			}
		}
		var event = window.event;
		var keyFlag = event == null ? false : ('13' == event.keyCode);
		if(inc != arrLen && ('save' == saveFlag || 'error' == saveFlag || keyFlag))
		{
			return false;
		}
		if(blankLen < (arrLen - inc))
		{
			return false;
		}
		return true;
	};
	
	qes.onlySameAs = function(fromQesCode, subQes){
		subQes = subQes == undefined ? '' : subQes;
		var arr = getQuestion(fromQesCode, subQes).notNullValues();
		var arrLen = arr.length;
		var vals = this.values();
		var valLen = vals.length;
		var inc = 0;
		for(var i = 0; i < valLen; i++)
		{
			for(var j = 0; j < arrLen; j++)
			{
				if(isNotNull(vals[i]) && !arrayInclude(arr, vals[i]))
				{
					return false;
				}
				else if(vals[i] == arr[j])
				{
					inc++;
					break;
				}
			}
		}
		var event = window.event;
		var keyFlag = event == null ? false : ('13' == event.keyCode);
		if(inc != arrLen && ('save' == saveFlag || 'error' == saveFlag || keyFlag))
		{
			return false;
		}
		return true;
	};
	
	qes.from = function(qesCode, count, subQes){
		count = count == undefined ? 1 : count;
		var thisAllValues = this.values();
		var allLen = thisAllValues.length;
		var thisValues = this.notNullValues();
		var fromValues = getQuestion(qesCode, subQes).notNullValues();
		var thisLen = thisValues.length;
		var fromLen = fromValues.length;
		var inc = 0;
		var blankLen = 0;
		for(var i = 0; i < allLen; i++)
		{
			if(!isNotNull(thisAllValues[i]))
			{
				blankLen++;
			}
		}
		for(var i = 0; i < thisLen; i++)
		{
			for(var j = 0; j < fromLen; j++)
			{
				if(thisValues[i] == fromValues[j])
				{
					inc++;
					break;
				}
			}
		}
		var event = window.event;
		var keyFlag = event == null ? false : ('13' == event.keyCode);
		if(inc < count && ('save' == saveFlag || 'error' == saveFlag || keyFlag))
		{
			return false;
		}
		if(blankLen < count - inc)
		{
			return false;
		}
		return true;
	};
	
	qes.onlyFrom = function(qesCode, count, subQes){
		count = count == undefined ? 1 : count;
		var thisAllValues = this.values();
		var allLen = thisAllValues.length;
		var thisValues = this.notNullValues();
		var fromValues = getQuestion(qesCode, subQes).notNullValues();
		var thisLen = thisValues.length;
		var fromLen = fromValues.length;
		var inc = 0;
		for(var i = 0; i < thisLen; i++)
		{
			for(var j = 0; j < fromLen; j++)
			{
				if(isNotNull(thisValues[i]) && !arrayInclude(fromValues, thisValues[i]))
				{
					return false;
				}
				else if(thisValues[i] == fromValues[j])
				{
					inc++;
					break;
				}
			}
		}
		var event = window.event;
		var keyFlag = event == null ? false : ('13' == event.keyCode);
		if(inc != count && ('save' == saveFlag || 'error' == saveFlag || keyFlag))
		{
			return false;
		}
		return true;
	};
	
	qes.toEnd = function(){
		endQes = qes.qesCode + '_' + qes.subQes;
		var focusHiddenEvents = $('input:hidden[name="focusHiddenEvents"]');
		var focusBakHiddenEvents = $('input:hidden[name="focusBakHiddenEvents"]');
		var focusLen = focusHiddenEvents.length;
		var x = 0;
		for(var i = 0; i < focusLen; i++)
		{
			var focusHiddenx = $(focusHiddenEvents.get(i));
			var fhid = focusHiddenx.attr('id');
			if(fhid.indexOf(endQes) > -1)
			{
				x = i;
				break;
			}
		}
		for(var i = x + 1; i < focusLen; i++)
		{
			var focusHiddenx = $(focusHiddenEvents.get(i));
			focusHiddenx.val('false;');
		}
		return false;
	};
	
	qes.recover = function(){
		var thisQes = qes.qesCode + '_' + qes.subQes;
		if(thisQes != endQes)
			return true;
		var focusHiddenEvents = $('input:hidden[name="focusHiddenEvents"]');
		var focusBakHiddenEvents = $('input:hidden[name="focusBakHiddenEvents"]');
		var focusLen = focusHiddenEvents.length;
		var x = 0;
		for(var i = 0; i < focusLen; i++)
		{
			var focusHiddenx = $(focusHiddenEvents.get(i));
			var fhid = focusHiddenx.attr('id');
			if(fhid.indexOf(endQes) > -1)
			{
				x = i;
				break;
			}
		}
		for(var i = x + 1; i < focusLen; i++)
		{
			var focusHiddenx = $(focusHiddenEvents.get(i));
			var focusBakHiddenx = $(focusBakHiddenEvents.get(i));
			focusHiddenx.val(focusBakHiddenx.val());
		}
		return true;
	};
	
	return qes;
}

function getCurrentTxt()
{
	var thisTxtId = txtArray[currentN];
	var thisTxt = $('#' + thisTxtId);
	return thisTxt;
}

//返回值，如果是false，则提示
function alertMsg(flag, msg, func)
{
	if(flag == false && ('save' == saveFlag || 'key' == saveFlag))
	{
		showMsg(msg, function(){
			if(func != undefined)
			{
				func();
			}
			getCurrentTxt().focus();
		});
	}
	else if(flag == false && 'error' == saveFlag)
	{
		var errorMsg = $('#errorMsg');
		if(errorMsg.length > 0)
		{
			errorMsg.val(errorMsg.val() + msg + '; ');
		}
	}
}


